package com.codeyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.domain.Order;
import com.codeyang.dto.OrderConfirmParam;
import com.codeyang.service.OrderService;
import com.codeyang.util.LoadUserInfoUtil;
import com.codeyang.vo.OrderResult;
import com.codeyang.vo.OrderStatusModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 描述: 订单接口管理
 *
 * @author CodeYang_Site
 * @version 2021/5/31 17:46
 */
@Api(tags = "订单接口管理")
@RestController
@RequestMapping("p/myOrder")
public class OrderController {


    @Autowired
    private OrderService orderService;


    @GetMapping("orderCount")
    @ApiOperation("查询用户订单数量")
    public ResponseEntity<OrderStatusModel> orderCount() {
        String userId = LoadUserInfoUtil.getUserId();
        OrderStatusModel statusModel = orderService.findUserOrderStatusCount(userId);
        return ResponseEntity.ok(statusModel);
    }

    @GetMapping("myOrder")
    @ApiOperation("分页查询用户的订单")
    public ResponseEntity<Page<Order>> myOrder(Page<Order> page, Order order) {
        String userId = LoadUserInfoUtil.getUserId();
        order.setUserId(userId);
        Page<Order> orderPage = orderService.findMyOrderByPage(page, order);
        return ResponseEntity.ok(orderPage);
    }


    @PostMapping("confirm")
    @ApiOperation("订单确认接口")
    public ResponseEntity<OrderResult> confirm(@RequestBody OrderConfirmParam orderConfirmParam) {
        String userId = LoadUserInfoUtil.getUserId();
        OrderResult orderResult = orderService.confirmOrder(userId, orderConfirmParam);
        return ResponseEntity.ok(orderResult);
    }


    @PostMapping("submit")
    @ApiOperation("生成预订单")
    public ResponseEntity<String> submit(@RequestBody OrderResult orderResult) {
        String userId = LoadUserInfoUtil.getUserId();
        String orderNumber = orderService.createOrderToPay(orderResult, userId);
        //这里使用雪花算法得到的订单id 112122552 太大丢到前端会被认为是 number类型会丢失精度,我们需要 转为str类型
        //这里 拼接一下转为 str
        return ResponseEntity.ok("orderNum:" + orderNumber);
    }

    @GetMapping("query")
    @ApiOperation("轮询查询数据库 订单状态")
    public ResponseEntity<Boolean> queryOrderStatus(@RequestParam String orderSn) {
        int count = orderService.count(new LambdaQueryWrapper<Order>()
                .eq(Order::getOrderNumber, orderSn)
                .eq(Order::getIsPayed, 1)
        );
        return ResponseEntity.ok(count > 0);
    }


    /**
     * 远程调用 - 根据订单号得到订单信息
     *
     * @param orderNumber
     * @return
     */
    @GetMapping("getOrderByOrderNumber")
    @ApiOperation("根据订单号得到订单信息")
    public Order getOrderByOrderNumber(@RequestParam String orderNumber) {
        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, orderNumber));
        return order;
    }


    @PostMapping("changeOrderStatus")
    @ApiOperation("远程调用,交易成功,修改订单状态")
    public void changeOrderStatus(@RequestParam String orderNum) {

        Order order = new Order();
        //设置订单状态 已经支付|待发货,修改DB
        order.setIsPayed(1);
        order.setStatus(2);
        order.setUpdateTime(new Date());
        boolean update = orderService.update(order, new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, orderNum));


    }
}
